#include "enemy_8.h"

extern int G_W, G_H;

ENEMY_8::ENEMY_8(SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    go = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_8/go.ogg",this);
    go->Play_Now();

    qsrand(QTime::currentTime().second());

    left_orientation=0+qrand()%(1-0+1);    // Розыграли ориентацию

    if(left_orientation)
        stvol=QPoint(101,68);
    else
        stvol=QPoint(11,66);

    point=20;

    if(left_orientation){
        border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_8/Enemy_8_l.poly");
        im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_8/Enemy_8_l.png");
    }
    else{
        border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_8/Enemy_8_r.poly");
        im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_8/Enemy_8_r.png");
    }

    this->setZValue(0.5);

    anim_enemy = new QGraphicsItemAnimation();

    enemy_time_line_1 = new QTimeLine();                                             // Скорость перемещения обьекта по траектории
    enemy_time_line_1->setEasingCurve( QEasingCurve::Type(1) );

    enemy_time_line_1->setFrameRange(0, 1);
    anim_enemy->setTimeLine(enemy_time_line_1);
    anim_enemy->setItem(this);

    i=0;

    shot=0; // выстрела ещё не было

    Move_1();

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(enemy_time_line_1,SIGNAL(finished()),SLOT(Inkriment()));

}

QPainterPath ENEMY_8::shape() const
{

    return *border;

}

QRectF ENEMY_8::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );

}

void ENEMY_8::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

ENEMY_8::~ENEMY_8()
{
    go->Stop();
}

int ENEMY_8::type() const
{

    return point;

}

void ENEMY_8::Move_1()
{

    enemy_time_line_1->setDuration(3000);

    if(left_orientation){
        start = QPoint(-200,0+qrand()%((G_H/2)-0+1));
        this->setPos(start);

        anim_enemy->setPosAt(0,start);
        anim_enemy->setPosAt( 1,QPoint(-50,start.y()));
    }
    else{
        start = QPoint(G_W,0+qrand()%((G_H/2)-0+1));
        this->setPos(start);

        anim_enemy->setPosAt(0,start);
        anim_enemy->setPosAt(1,QPoint(G_W-100,start.y()));
    }

    enemy_time_line_1->start();

}

void ENEMY_8::Move_2()
{

    enemy_time_line_1->setDuration(300);

    if(left_orientation){
        anim_enemy->setPosAt(0,this->pos());
        connect(enemy_time_line_1,SIGNAL(valueChanged(qreal)),SLOT(Fire() ));  // Нужен момент выстрела
        anim_enemy->setPosAt(0.5,QPoint(this->pos().x()-20,this->pos().y()));
        anim_enemy->setPosAt(1,QPoint(this->pos().x()+20,this->pos().y()) );
    }
    else{
        anim_enemy->setPosAt(0,this->pos());
        connect(enemy_time_line_1,SIGNAL(valueChanged(qreal)),SLOT(Fire() ));  // Нужен момент выстрела
        anim_enemy->setPosAt(0.5,QPoint(this->pos().x()+20,this->pos().y()));
        anim_enemy->setPosAt(1,QPoint(this->pos().x()-20,this->pos().y()));
    }

    enemy_time_line_1->start();

}

void ENEMY_8::Move_3()
{

    enemy_time_line_1->setDuration(1000);

    anim_enemy->setPosAt(0,this->pos());
    anim_enemy->setPosAt(1,start);

    enemy_time_line_1->start();

    connect(enemy_time_line_1,SIGNAL(finished()),SLOT(deleteLater()));      // Удаляем здесь

}

void ENEMY_8::Inkriment()
{

    if(i==0) Move_2();

    if(i==1) Move_3();

    ++i;

}

void ENEMY_8::Fire()
{

    ++shot;

    if(shot==2){
        emit Fire(QPoint(this->pos().x()+stvol.x(),this->pos().y()+stvol.y()),left_orientation);    // отдать коры и ориентацию для выстрела
    }

}

void ENEMY_8::Damage_Detected(QPointF a, int d)
{

    a = QPointF();
    point-=d;

    if(point<=0)
        if(this->scene()){
            disconnect(enemy_time_line_1,SIGNAL(finished()),this,SLOT(deleteLater()));
            go->Stop();
            emit Kill_Me_Please((QGraphicsObject*)this);
        }

}



